package com.frame.easy.modular.activiti.controller;

import com.frame.easy.base.controller.BaseController;
import com.frame.easy.common.constant.CommonConst;
import com.frame.easy.modular.activiti.model.Process;
import com.frame.easy.modular.activiti.service.ProcessService;
import com.frame.easy.result.Tips;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 流程
 *
 * @author tengchong
 * @date 2019-07-12
 */
@Controller
@RequestMapping("/auth/activiti/process")
public class ProcessController extends BaseController {
    /**
     * view 路径
     */
    private static final String PREFIX = "modular/activiti/process/";

    @Autowired
    private ProcessService service;

    /**
     * 列表
     *
     * @return String
     */
    @RequestMapping("list")
    @RequiresPermissions("activiti:process:select")
    public String list() {
        logger.debug("/auth/activiti/process/list");
        return PREFIX + "list";
    }

    /**
     * 查询数据
     *
     * @param process 查询条件
     * @return 数据
     */
    @RequestMapping("select")
    @ResponseBody
    @RequiresPermissions("activiti:process:select")
    public Tips select(@RequestBody(required = false) Process process) {
        logger.debug("/auth/activiti/process/select");
        return Tips.getSuccessTips(service.select(process));
    }

    /**
     * 删除
     *
     * @param ids 数据ids
     * @return true/false
     */
    @RequestMapping("delete/{ids}")
    @ResponseBody
    @RequiresPermissions("activiti:process:delete")
    public Tips delete(@PathVariable("ids") String ids) {
        logger.debug("/auth/activiti/delete/" + ids);
        return Tips.getSuccessTips(service.delete(ids));
    }

    /**
     * 流程转模型
     *
     * @param id 流程id
     * @return true/false
     */
    @RequestMapping("convert/{id}/to/model")
    @ResponseBody
    @RequiresPermissions("activiti:model:save")
    public Tips convertToModel(@PathVariable("id") String id) {
        logger.debug("/auth/activiti/process/convert/" + id + "/to/model");
        return Tips.getSuccessTips(service.convertToModel(id));
    }

    /**
     * 查看流程图片
     *
     * @param id           流程id
     * @param resourceName 资源名称
     * @return true/false
     */
    @RequestMapping("get/{id}/{resourceName}/image/stream")
    @RequiresPermissions("activiti:process:select")
    public void getImageStream(@PathVariable("id") String id,
                               @PathVariable("resourceName") String resourceName, HttpServletResponse response) throws IOException {
        logger.debug("/auth/activiti/process/get/" + id + "/" + resourceName + "/image/stream");
        InputStream inputStream = service.getImageStream(id, resourceName);
        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes, 0, CommonConst.BYTE1024)) != -1) {
            response.getOutputStream().write(bytes, 0, len);
        }
    }

    /**
     * 添加流程
     *
     * @param path 流程模型文件路径
     * @return 保存后信息
     */
    @RequestMapping("add")
    @ResponseBody
    @RequiresPermissions("activiti:process:add")
    public Tips add(String path) {
        logger.debug("/auth/activiti/process/add");
        return Tips.getSuccessTips(service.add(path));
    }

    /**
     * 挂起流程
     *
     * @param processDefinitionId     流程id
     * @param suspendProcessInstances 如果为true，将同时挂起此流程正在进行的任务
     * @return tips
     */
    @RequestMapping("suspend/{processDefinitionId}/{suspendProcessInstances}")
    @ResponseBody
    @RequiresPermissions("activiti:process:suspend")
    public Tips suspend(@PathVariable("processDefinitionId") String processDefinitionId,
                        @PathVariable("suspendProcessInstances") boolean suspendProcessInstances) {
        logger.debug("/auth/activiti/process/suspend/" + processDefinitionId + "/" + suspendProcessInstances);
        service.suspend(processDefinitionId, suspendProcessInstances);
        return Tips.getSuccessTips();
    }

    /**
     * 激活流程
     *
     * @param processDefinitionId     流程id
     * @param suspendProcessInstances 如果为true，将同时激活此流程正在进行的任务
     * @return tips
     */
    @RequestMapping("activation/{processDefinitionId}/{suspendProcessInstances}")
    @ResponseBody
    @RequiresPermissions("activiti:process:activation")
    public Tips activation(@PathVariable("processDefinitionId") String processDefinitionId,
                        @PathVariable("suspendProcessInstances") boolean suspendProcessInstances) {
        logger.debug("/auth/activiti/process/activation/" + processDefinitionId + "/" + suspendProcessInstances);
        service.activation(processDefinitionId, suspendProcessInstances);
        return Tips.getSuccessTips();
    }

    /**
     * 查询流程数据用于放到select的option中
     *
     * @return tips
     */
    @RequestMapping("select/process/for/select")
    @ResponseBody
    public Tips selectProcessForSelect() {
        logger.debug("select/process/for/select");
        return Tips.getSuccessTips(service.selectProcessForSelect());
    }


}
